package com.controller;

import com.model.Const;
import com.model.Page;
import com.model.Result;
import com.model.company;
import com.service.CompanyService;
import com.util.MD5Util;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/9/6 0006.
 */
@Controller
public class companyController {
    @Resource
    private CompanyService CompanyService;
    MD5Util MD5Util =new MD5Util();
    @RequestMapping(value = "getCompanyList.do", method = RequestMethod.GET)
    public  String getCompanyListdo(HttpServletRequest request,
                                    @RequestParam(value = "companyName", required = false) String companyName,
                                    @RequestParam(value = "legalPerson", required = false) String legalPerson,
                                    @RequestParam(value = "phone", required = false) String phone,
                                    @RequestParam(value = "pageNow", required = false, defaultValue = "1") Integer pageNow){
        int count =CompanyService.selectCompanyCount(new company(companyName,phone,legalPerson));
        Page page = new Page(count, pageNow);
        List<company> companyList=CompanyService.getCompanyListdo(new company(companyName,phone,legalPerson,page.getStartPos(), page.getPageSize()));
        request.setAttribute("phone", phone);
        request.setAttribute("companyName", companyName);
        request.setAttribute("legalPerson", legalPerson);
        request.setAttribute("page", page);
        request.setAttribute("companyList", companyList);
        return "company";
    }
    @RequestMapping(value = "insertCompany.do", method = RequestMethod.POST)
    public  String  insertCompany(HttpServletRequest request, HttpSession session,company company){
        Date date = new Date();
        //创建时间
        company.setCreateTime(date.getTime()/1000);
        String username =(String) session.getAttribute(Const.SESSION_USERNAME);
        company.setManagerName(username);
        //加密密码
        String passowrd = MD5Util.transMD5(company.getPassword()+"BackOfPinChengCloud");
        company.setPassword(passowrd);
        //添加
        CompanyService.insertCompany(company);
        return  getCompanyListdo(request,null,null,null,1);
    }
    @RequestMapping(value = "updateCompany.do", method = RequestMethod.POST)
    public  String  updateCompany(HttpServletRequest request, HttpSession session,company company){
        //添加
        CompanyService.modifyCompany(company);
        return  getCompanyListdo(request,null,null,null,1);
    }
    @RequestMapping(value = "updateCompanyPassword.do", method = RequestMethod.POST)
    public  String  updateCompanyPassword(HttpServletRequest request, HttpSession session,company company){
        if(company.getPassword()!=""){
            String passowrd = MD5Util.transMD5(company.getPassword()+"BackOfPinChengCloud");
            company.setPassword(passowrd);
        }
        CompanyService.modifyCompanyPassword(company);
        return  getCompanyListdo(request,null,null,null,1);
    }
    @RequestMapping(value = "checkCompanyName", method = RequestMethod.GET)
    @ResponseBody
    public Result checkCompanyName(String CompanyName){
        System.out.println("CompanyName"+CompanyName);
        boolean isornot=CompanyService.isCompanyName(CompanyName);
        if(isornot){
            return  new Result(0,"企业名称已经存在了",null);
        }else{
            return  new  Result(1,"企业名称通过检测",null);
        }
    }
    @RequestMapping(value = "checkCompanyuserName", method = RequestMethod.GET)
    @ResponseBody
    public Result checkCompanyuserName(String username){
        boolean isornot=CompanyService.isCompanyName(username);
        if(isornot){
            return  new Result(0,"企业端账号已经存在了",null);
        }else{
            return  new  Result(1,"企业端账号通过检测",null);
        }
    }
}
